package com.srit.core.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.srit.core.bean.Page;
import com.srit.core.ibatis.dao.SqlMapDao;
import com.srit.core.service.BaseService;

public class BaseServiceImpl implements BaseService {
	static Logger log = LoggerFactory.getLogger(BaseServiceImpl.class);
	private SqlMapDao sqlMapDao;

	public void setSqlMapDao(SqlMapDao sqlMapDao) {
		this.sqlMapDao = sqlMapDao;
	}

	private String getEntityName(Class clz) {
		return clz.getName().substring(clz.getName().lastIndexOf(".") + 1)
				.trim();
	}

	@Override
	public void save(Object object) {
		log.debug("save");
		sqlMapDao.save(this.getEntityName(object.getClass()) + ".save", object);
	}

	@Override
	public void update(Object object) {
		sqlMapDao.update(this.getEntityName(object.getClass()) + ".update",
				object);
	}

	@Override
	public Object getById(Object object, Integer id) {
		return sqlMapDao.getById(this.getEntityName(object.getClass())
				+ ".getById", id);
	}

	@Override
	public void page(Object object, Page page) {
		sqlMapDao.queryForPage(this.getEntityName(object.getClass()) + ".page",
				page);

	}

	@Override
	public void delete(Object object) {
		sqlMapDao.delete(this.getEntityName(object.getClass()) + ".delete",
				object);
	}

}
